package com.jy.appmarket.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2014年8月11日
 * @version 1.0
 */

public class BadgeUtils {
	private static String TAG = "BadgeUtils";

	/**
	 * 仅支持小米和三星ROM 现在只对三星进行设置
	 **/
	public static boolean isSupportBadge() {
		// return Build.MANUFACTURER.equalsIgnoreCase("samsung");
		return Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")
				|| Build.MANUFACTURER.equalsIgnoreCase("samsung");
	}

	// private static boolean isSamsungSupport(Context context) {
	// if (!Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
	// return false;
	// }
	// Cursor cur =
	// context.getContentResolver().query(Uri.parse("content://com.sec.badge/apps"),
	// null, null,
	// null, null);
	// if(cur == null){
	// return false;
	// }
	// cur.close();
	// return true;
	// }

	public static void showBadge(Context context, String pkg, String cls) {
		if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
			String component = pkg + "/" + cls;
			showXiaomiBadge(context, component, "New");
		} else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
			showSamsungBadge(context, pkg, cls, 1);
		} else {
			LogUtil.d(TAG, "unsupported platform");
		}
	}

	public static void clearBadge(Context context, String pkg, String cls) {
		if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
			String component = pkg + "/" + cls;
			showXiaomiBadge(context, component, null);
		} else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
			showSamsungBadge(context, pkg, cls, 0);
		} else {
			LogUtil.d(TAG, "unsupported platform");
		}
	}

	private static void showSamsungBadge(Context context, String pkg, String cls, int count) {
		if (TextUtils.isEmpty(pkg) || TextUtils.isEmpty(cls)) {
			return;
		}
		Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
		intent.putExtra("badge_count", count);
		intent.putExtra("badge_count_package_name", pkg);
		intent.putExtra("badge_count_class_name", cls);
		context.sendBroadcast(intent);
	}

	public static void showXiaomiBadge(Context context, String component, String text) {
		Intent intent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
		intent.putExtra("android.intent.extra.update_application_component_name", component);
		intent.putExtra("android.intent.extra.update_application_message_text", text);
		context.sendBroadcast(intent);
	}

	/**
	 * 获取当前应用程序Launch class
	 * 
	 * @param context
	 * @return Launch class name;
	 */
	public static String getLaunchClass(Context context) {
		return getLaunchClass(context, context.getApplicationInfo().packageName);
	}

	/**
	 * 获取指定包名应用程序的Launch class
	 * 
	 * @param context
	 * @param packageName
	 * @return 包名存在，返回Launch class name; 包名不存在，返回"";
	 */
	public static String getLaunchClass(Context context, String packageName) {
		String cls = "";
		PackageManager pm = context.getPackageManager();
		Intent intent = pm.getLaunchIntentForPackage(packageName);
		if (intent != null) {
			cls = intent.getComponent().getClassName();
		}
		return cls;
	}

}
